<!-- 老师简历 -->
<template>
	<view class="container">
		<view class="">
			<u-alert title="当前简历状态为:已发布" type="success" :show-icon="true" v-if="issueStatus"></u-alert>
			<u-alert title="当前简历状态为:未发布" type="error" :show-icon="true" v-else></u-alert>
		</view>
		<view class="c-form">
			<view class="form-info">
				<u-form :model="resumeInfo" ref="form" :rules="rules">
					<u-form-item label="姓名" prop="name">
						<u-input placeholder="输入姓名" v-model="resumeInfo.name" :disabled="isDisabled"></u-input>
					</u-form-item>
					<u-form-item label="性别" prop="sex">
						<u-radio-group v-model="resumeInfo.sex" :disabled="isDisabled">
							<u-radio label="男" :name="1"></u-radio>
							<u-radio :customStyle="{marginLeft:'40rpx'}" label="女" :name="2"></u-radio>
						</u-radio-group>
					</u-form-item>
					<u-form-item label="电话" prop="tel">
						<u-input placeholder="联系电话" v-model="resumeInfo.tel" :disabled="isDisabled"></u-input>
					</u-form-item>
					<u-form-item label="邮箱" prop="email">
						<u-input placeholder="邮箱地址" v-model="resumeInfo.email" :disabled="isDisabled"></u-input>
					</u-form-item>
					<u-form-item label="毕业院校" prop="school">
						<u-input placeholder="毕业院校名称" v-model="resumeInfo.school" :disabled="isDisabled"></u-input>
					</u-form-item>
					<u-form-item label="学历" prop="education">
						<u-input placeholder="学历" v-model="resumeInfo.education" :disabled="isDisabled"></u-input>
					</u-form-item>
					<u-form-item label="成绩/荣誉" prop="certificate">
						<u-input placeholder="获得过的成绩/荣誉" v-model="resumeInfo.certificate" :disabled="isDisabled"></u-input>
					</u-form-item>
					<u-form-item label="工作经历" prop="experience">
						<u-textarea v-model="resumeInfo.experience" placeholder="工作经历简介" autoHeight
							:disabled="isDisabled"></u-textarea>
					</u-form-item>
				</u-form>
			</view>
		</view>
		<view class="m-btn">
			<view class="btn-block">
				<u-button type="primary" text="撤销发布" @click="saveResume"></u-button>
			</view>
			<view class="btn-block">
				<u-button type="primary" text="保存并发布" @click="issueResume"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "",
		data() {
			return {
				// 简历发布状态
				issueStatus: false,
				// 是否禁填
				isDisabled: false,
				// 是否已有简历信息
				isResume: false,
				// 简历信息
				resumeInfo: {
					user_id: '',
					name: null,
					tel: null,
					email: null,
					sex: null,
					school: null,
					education: null,
					certificate: null,
					experience: null
				},
				// 表单校验
				rules: {
					'name': {
						required: true,
						message: '请填写姓名',
						trigger: ['blur']
					},
					'sex': {
						type: 'number',
						required: true,
						message: '请选择性别',
						trigger: ['change']
					},
					'tel': {
						required: true,
						message: '请填写联系电话',
						trigger: ['blur']
					},

					'school': {
						required: true,
						message: '请填写毕业院校',
						trigger: ['blur']
					},
					'email': {
						required: true,
						message: '请填写邮箱地址',
						trigger: ['blur']
					},
					'education': {
						required: true,
						message: '请填写学历',
						trigger: ['blur']
					},
					'certificate': {
						required: true,
						message: '请填写获得证书',
						trigger: ['blur']
					},
					'experience': {
						required: true,
						message: '请填写工作经历',
						trigger: ['blur']
					}
				}
			}
		},
		onLoad() {
			this.getTeacherResume()
		},
		onReady() {
			//onReady 为uni-app支持的生命周期之一
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			// 获取老师信息
			getTeacherInfo() {
				this.$http.getTeacherInfo().then(res => {
					let {
						name,
						sex,
						email,
						mobile: tel,
						grad_school: school
					} = res

					this.resumeInfo = {
						user_id: '',
						name,
						tel,
						email,
						sex,
						school,
						education: null,
						certificate: null,
						experience: null
					}
				})
			},
			// 获取老师简历
			getTeacherResume() {
				this.$http.getTeacherResume().then(res => {
					if (res) {
						this.resumeInfo = res
						if (res.status == 1) {
							this.issueStatus = true
						} else {
							this.issueStatus = false

						}
					} else {
						this.getTeacherInfo()
						this.issueStatus = false
						this.isDisabled = false
					}
				}).catch(err => {
					uni.$u.toast(err.msg)
				})
			},
			// 发布简历
			issueResume() {
				this.$refs.form.validate().then(res => {
					this.$http.updateTeacherResumeStatus({
						...this.resumeInfo,
						status: 1
					}).then(res => {
						this.issueStatus = true
						uni.$u.toast("已发布")
					}).catch(err => {
						uni.$u.toast(err.msg)
					})
				})
			},
			// 撤销发布
			saveResume() {
				this.$http.updateTeacherResumeStatus({
					...this.resumeInfo,
					status: 2
				}).then(res => {
					this.issueStatus = false
					uni.$u.toast("已撤销")
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.container {
		.c-form{
			background-color: #fff;
			padding: 10rpx 20rpx;
			box-sizing: border-box;
		}
	}
</style>
